<?php
/**
 * 发送文件
 *
 * @author: legend(legendsky@hotmail.com)
 * @link: http://www.ugia.cn/?p=109
 * @description: send file to client
 * @version: 1.0
 *
 * @param string   $fileName      文件名称或路径
 * @param string   $fancyName     自定义的文件名,为空则使用filename
 * @param boolean  $forceDownload 是否强制下载
 * @param integer  $speedLimit    速度限制,单位为字节,0为不限制,不支持windows服务器
 * @param string   $$contentType  文件类型,默认为application/octet-stream
 *
 * @return boolean
 */
function sendFile($fileName, $fancyName = '', $forceDownload = true, $speedLimit = 0, $contentType = '') {
	if (! is_readable ( $fileName )) {
		header ( "HTTP/1.1 404 Not Found" );
		return false;
	}
	$fileStat = stat ( $fileName );
	$lastModified = $fileStat ['mtime'];
	$md5 = md5 ( $fileStat ['mtime'] . '=' . $fileStat ['ino'] . '=' . $fileStat ['size'] );
	$etag = '"' . $md5 . '-' . crc32 ( $md5 ) . '"';
	header ( 'Last-Modified: ' . gmdate ( "D, d M Y H:i:s", $lastModified ) . ' GMT' );
	header ( "ETag: $etag" );
	if (isset ( $_SERVER ['HTTP_IF_MODIFIED_SINCE'] ) && strtotime ( $_SERVER ['HTTP_IF_MODIFIED_SINCE'] ) >= $lastModified) {
		header ( "HTTP/1.1 304 Not Modified" );
		return true;
	}
	if (isset ( $_SERVER ['HTTP_IF_UNMODIFIED_SINCE'] ) && strtotime ( $_SERVER ['HTTP_IF_UNMODIFIED_SINCE'] ) < $lastModified) {
		header ( "HTTP/1.1 304 Not Modified" );
		return true;
	}
	if (isset ( $_SERVER ['HTTP_IF_NONE_MATCH'] ) && $_SERVER ['HTTP_IF_NONE_MATCH'] == $etag) {
		header ( "HTTP/1.1 304 Not Modified" );
		return true;
	}
	if ($fancyName == '') {
		$fancyName = basename ( $fileName );
	}
	if ($contentType == '') {
		$contentType = 'application/octet-stream';
	}
	$fileSize = $fileStat ['size'];
	$contentLength = $fileSize;
	$isPartial = false;
	if (isset ( $_SERVER ['HTTP_RANGE'] )) {
		if (preg_match ( '/^bytes=(\d*)-(\d*)$/', $_SERVER ['HTTP_RANGE'], $matches )) {
			$startPos = $matches [1];
			$endPos = $matches [2];
			if ($startPos == '' && $endPos == '') {
				return false;
			}
			if ($startPos == '') {
				$startPos = $fileSize - $endPos;
				$endPos = $fileSize - 1;
			} else if ($endPos == '') {
				$endPos = $fileSize - 1;
			}
			$startPos = $startPos < 0 ? 0 : $startPos;
			$endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;
			$length = $endPos - $startPos + 1;
			if ($length < 0) {
				return false;
			}
			$contentLength = $length;
			$isPartial = true;
		}
	}
	// send headers
	if ($isPartial) {
		header ( 'HTTP/1.1 206 Partial Content' );
		header ( "Content-Range: bytes $startPos-$endPos/$fileSize" );
	} else {
		header ( "HTTP/1.1 200 OK" );
		$startPos = 0;
		$endPos = $contentLength - 1;
	}
	header ( 'Pragma: cache' );
	header ( 'Cache-Control: public, must-revalidate, max-age=0' );
	header ( 'Accept-Ranges: bytes' );
	header ( 'Content-type: ' . $contentType );
	header ( 'Content-Length: ' . $contentLength );
	if ($forceDownload) {
		header ( 'Content-Disposition: attachment; filename="' . rawurlencode ( $fancyName ) . '"' );
	}
	header ( "Content-Transfer-Encoding: binary" );
	$bufferSize = 2048;
	if ($speedLimit != 0) {
		$packetTime = floor ( $bufferSize * 1000000 / $speedLimit );
	}
	$bytesSent = 0;
	$fp = fopen ( $fileName, "rb" );
	fseek ( $fp, $startPos );
	//fpassthru($fp);
	while ( $bytesSent < $contentLength && ! feof ( $fp ) && connection_status () == 0 ) {
		if ($speedLimit != 0) {
			list ( $usec, $sec ) = explode ( " ", microtime () );
			$outputTimeStart = (( float ) $usec + ( float ) $sec);
		}
		$readBufferSize = $contentLength - $bytesSent < $bufferSize ? $contentLength - $bytesSent : $bufferSize;
		$buffer = fread ( $fp, $readBufferSize );
		echo $buffer;
		ob_flush ();
		flush ();
		$bytesSent += $readBufferSize;
		if ($speedLimit != 0) {
			list ( $usec, $sec ) = explode ( " ", microtime () );
			$outputTimeEnd = (( float ) $usec + ( float ) $sec);
			$useTime = (( float ) $outputTimeEnd - ( float ) $outputTimeStart) * 1000000;
			$sleepTime = round ( $packetTime - $useTime );
			if ($sleepTime > 0) {
				usleep ( $sleepTime );
			}
		}
	}
	return true;
}
//sendFile('../../files/hotelcalifornia.mp3', 'outside_great_distance.mp3');
//session_start();
//unset($_SESSION[code]);
//$code=substr(md5(rand()),10,4);
//$_SESSION[code]=$code;
//$font = new SWFFont( '_sans' );    //设置文字显示的字体，这个在支持自定义字体方面还比较麻烦，现在使用系统字体吧。
//$tf = new SWFTextField();             //设置文本域
//$tf->setFont( $font );                   //指定文本使用的字体
//$tf->setColor( rand(90,255),rand(90,255),rand(90,255) );   //文本的颜色
//$tf->setHeight( 80 );                //文本高度
//$tf->addString( $code);            //添加文本内容
//$m = new SWFMovie();            //创建一个flash文件
//$m->setbackground( 60,60,60 );   //设置背景颜色
//$m->setDimension( 250, 80 );       //设置flash的大小
//$f_tf = $m->add( $tf );               //将文本域添加到影片中
//$f_tf->moveTo( -1000, 0 );         //将文本对象移到影片左边，-1000应该让大家看不到的了。
//for( $i = 0; $i < 31; $i++ ) {      
//$m->nextframe();                     //影片播放下一帧
//if ($i>25)
//$f_tf->moveTo( ($i-10)*($i-10), 0 );   //将文本对象加速向右移动，飞出右边框
//else if($i>5)
//$f_tf->moveTo( ($i-5)*4, 0 );            //慢点移动，好让大家看清文本内容
//else
//$f_tf->moveTo( -($i-15)*($i-15), 0 );   //迅速从左边飞到中间
//}
//header('Content-type: application/x-shockwave-flash');  //设置输出格式
//$m->output();                                          //将影片输出


class validate_robot {
	function flash_code() {
		session_start ();
		unset ( $_SESSION ['num'] );
		$num = substr ( md5 ( rand () ), 10, 4 );
		$_SESSION ['num'] = $num;
		$font = new SWFFont ( '_sans' );
		$tf = new SWFTextField ( );
		$tf->setFont ( $font );
		$tf->setColor ( rand ( 90, 255 ), rand ( 90, 255 ), rand ( 90, 255 ) );
		$tf->setHeight ( 80 );
		$tf->addString ( $num );
		
		$m = new SWFMovie ( );
		$m->setbackground ( 60, 60, 60 );
		$m->setDimension ( 250, 80 );
		
		$f_tf = $m->add ( $tf );
		$f_tf->moveTo ( - 1000, 0 );
		
		for($i = 0; $i < 31; $i ++) {
			$m->nextframe ();
			if ($i > 25)
				$f_tf->moveTo ( ($i - 10) * ($i - 10), 0 );
			else if ($i > 5)
				$f_tf->moveTo ( ($i - 5) * 4, 0 );
			else
				$f_tf->moveTo ( - ($i - 15) * ($i - 15), 0 );
		}
		
		header ( 'Content-type: application/x-shockwave-flash' );
		$m->output ();
	}
}
?>